import time

from selenium.webdriver.common.by import By

from common.Utils import BlogDriver
#测试博客登录页面

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "登录页面网址"
        self.driver = BlogDriver.driver
        #BlogDriver.driver.get(self.url)
        self.driver.get(self.url)
    #成功登录测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #能够找到博客首页用户的昵称，说明登录成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR, "昵称标签")
        BlogDriver.getScreeShot()
        #返回到登录页面
        self.driver.back()

    #异常登录测试用例
    def LoginFailTest(self):
        #若连续多次的send_keys则会出现关键词拼接，而不是替换，若要替换需要先clear
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        #错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #检测是否登录失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "body").text
        #添加屏幕截图
        BlogDriver.getScreeShot()
        #断言检测一下是否符合预期
        assert actual == "密码错误"


login = BlogLogin()
login.LoginSucTest()
login.LoginFailTest()